﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.Tools.Extensions
 * 接口名称 ObjectExtension
 * 开发人员：ADMIN
 * 创建时间：2024/1/6 14:12:56
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Tools.Extensions
{
	/// <summary>
	/// 
	/// </summary>
	public static class ObjectExtension
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="o"></param>
		/// <returns></returns>
        public static byte ToByte(this object o)
        {
            byte i =Byte.MinValue;
			i--;
            if (o == null)
                return i;
			if (o is byte value)
            {
                i = value;
			}
			else
			{
				i = StringExtension.ToByte(o.ToString());
			}

			return i;
        }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="o"></param>
		/// <returns></returns>
		public static bool ToBool(this object o)
		{
			bool i = false;
			if (o == null)
				return i;

			if (o is bool s)
			{
				i = s;
			}
			return i;
		}

		/// <summary>
		/// object convert to int
		/// </summary>
		/// <param name="o"></param>
		/// <returns></returns>
		public static int ToInt(this object o)
		{

			int i=-1;
			if (o == null)
				return i;
			 if (o is int value)
			{
				i=value;
			}
			else
			{
				i = StringExtension.ToInt(o.ToString());
			}
			return i ;
		}

        /// <summary>
        /// object convert to long
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static long ToInt64(this object o)
		{
			long i = -1;
			if (o == null)
				return i;
			if(o is long value)
			{
				i = value;
			}else {
				i = StringExtension.ToInt64(o.ToString());
			}
			return i;
		}

        /// <summary>
        /// object convert to float
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static float ToFloat(this object o)
		{
			float i = -1;
			if (o == null)
				return i;
			if (o is float value)
			{
				i = value;
			}
			else
			{
				i = StringExtension.ToFloat(o.ToString());
			}
		
			return i;
		}

        /// <summary>
        /// object convert to double
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static double ToDouble(this object o)
		{
			double i = -1;
			if (o == null)
				return i;
			if (o is double value)
			{
				i = value;
			}
			else
			{
				i = StringExtension.ToDouble(o.ToString());
			}
			return i;
		}

        /// <summary>
        /// object convert to decimal
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object o)
		{
			decimal i = -1;
			if (o == null)
				return i;
			if (o is decimal value)
			{
				i = value;
			}
			else
			{
				i = StringExtension.ToDecimal(o.ToString());
			}
			return i;
		}

        /// <summary>
        /// object convert to guid
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>

        public static Guid ToGuid(this object o)
		{
			Guid i = Guid.Empty;
			if (o == null)
				return i;
			if (o is Guid value)
			{
				i = value;
			}
			else
			{
				i = StringExtension.ToGuid(o.ToString());
			}
			return i;
		}

        /// <summary>
        /// object convert to DateTime
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object o)
		{
			DateTime i = DateTime.MinValue;
			if (o == null)
				return i;
			if (o is DateTime value)
			{
				i = value;
			}
			else
			{
				i = StringExtension.ToDateTime(o.ToString());
			}
			return i;
		}

        /// <summary>
        /// object convert to TimeSpan
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static TimeSpan ToTimeSpan(this object o)
		{
			TimeSpan i =TimeSpan.Zero;
			if (o == null)
				return i;
			if (o is TimeSpan value)
			{
				i = value;
			}
			else
			{
				i = StringExtension.ToTimeSpan(o.ToString());
			}
			return i;
		}

        /// <summary>
        /// object convert to short
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static short ToShort(this object o)
        {
            short i=0;
            if (o == null)
                return i;
            if (o is short value)
            {
                i = value;
			}
			else
			{
				StringExtension.ToShort(o.ToString());
			}
            return i;
        }
    }
}
